package com.lucher.mes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.sound.sampled.Line;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lucher.mes.common.Tools;
import com.lucher.mes.dao.BomMapper;
import com.lucher.mes.dao.ProduceinfoMapper;
import com.lucher.mes.dao.ProducematerialMapper;
import com.lucher.mes.dao.ProductMapper;
import com.lucher.mes.dao.WorklineMapper;
import com.lucher.mes.po.Bom;
import com.lucher.mes.po.Prisoner;
import com.lucher.mes.po.Produceinfo;
import com.lucher.mes.po.Product;
import com.lucher.mes.po.Workline;
import com.lucher.mes.service.IProduceinfoService;

@Controller
@RequestMapping("/produceinfo")  
public class ProduceinfoController extends BaseController{
	@Autowired
	private ProduceinfoMapper produceinfoDao;
	@Autowired
	private ProductMapper productDao;
	@Autowired
	private WorklineMapper worklineDao;
	@Autowired
	private IProduceinfoService produceinfoService;
	@Autowired
	private ProducematerialMapper producematerialDao;
	@Autowired
	private BomMapper bomDao;
	
	/**
	 * 查询信息
	 */
	@RequestMapping(value = "/findByList")
	public String findByList(Produceinfo form) {
		return "produceinfo/findProduceinfoList";
	}
	
	/**
	 * 查询信息Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findListJson")
	public Map<String, Object> findListJson(Produceinfo form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		Integer total = produceinfoDao.findByCount(form);
		List<Produceinfo> list = produceinfoDao.findByListForPage(form);
		
		for (Produceinfo produceinfo : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			
			objectMap.put("id", produceinfo.getId());
			objectMap.put("producecode", produceinfo.getProducecode());
			objectMap.put("productcode", produceinfo.getProductcode());
			objectMap.put("productname", produceinfo.getProductname());
			objectMap.put("amount", produceinfo.getAmount());
			objectMap.put("installinfo", produceinfo.getInstallinfo());
			objectMap.put("producercode", produceinfo.getProducecode());
			objectMap.put("operatorcode", produceinfo.getOperatorcode());
			objectMap.put("addtime", StringUtils.isEmpty(produceinfo.getAddtime())?"":Tools.getStr(produceinfo.getAddtime()).substring(0, 19));
			objectMap.put("linecode", produceinfo.getLinecode());
			objectMap.put("linename", produceinfo.getLinename());
			objectMap.put("onlinetime", StringUtils.isEmpty(produceinfo.getOnlinetime())?"":Tools.getStr(produceinfo.getOnlinetime()).substring(0, 19));
			objectMap.put("offlinetime", StringUtils.isEmpty(produceinfo.getOfflinetime())?"":Tools.getStr(produceinfo.getOfflinetime()).substring(0, 19));
			objectMap.put("status", produceinfo.getStatus());
			objectMap.put("outamount", produceinfo.getOutamount());
			objectMap.put("outstatus", produceinfo.getOutstatus());
			objectMap.put("remark", produceinfo.getRemark());
			
			objectlist.add(objectMap);
		}
		
		result.put("count", total);
		result.put("data", objectlist);
		return result;
	}

	/**
	 * 添加信息初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/addInit")
	public String addInit(Produceinfo form,Model model, Product record, Workline workline) {
		model.addAttribute("productlist", productDao.findByList(record));
		model.addAttribute("worklinelist", worklineDao.findByList(workline));
		return "produceinfo/addProduceinfo";
	}

	/**
	 * 新增
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/save")
	public Map<String,Object>  save(Produceinfo form) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getProductcode())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，请选择产品");
			return responseJson;
		} else {
			Bom bomForm = new Bom();
			bomForm.setProductcode(form.getProductcode());
			List<Bom> bomList = bomDao.findByList(bomForm);
			if (bomList == null || bomList.size() == 0) {
				responseJson.put("flag", "0");//添加失败
				responseJson.put("msg", "操作失败，该产品没有BOM清单");
				return responseJson;
			}
		}
		
		//保存信息
		Map<String, Object> resultHp = produceinfoService.saveProduceinfo(form);
		
		responseJson.put("flag", resultHp.get("flag"));//操作成功
		responseJson.put("msg",  resultHp.get("msg"));
		
		return responseJson;
	}

	/**
	 * 更新初始化
	 */
	@RequestMapping(value = "/updateInit")
	public String updateInit(Produceinfo form,Model model, Product record, Workline workline) {
		model.addAttribute("productlist", productDao.findByList(record));
		model.addAttribute("worklinelist", worklineDao.findByList(workline));
		
		Produceinfo produceinfo = produceinfoDao.selectByPrimaryKey(form.getId());
		form.setProduceinfo(produceinfo);

		return "produceinfo/updateProduceinfo";
	}

	/**
	 * 更新
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/update")
	public Map<String,Object> update(Produceinfo form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getProductcode())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，请选择产品");
			return responseJson;
		} else {
			Bom bomForm = new Bom();
			bomForm.setProductcode(form.getProductcode());
			List<Bom> bomList = bomDao.findByList(bomForm);
			if (bomList == null || bomList.size() == 0) {
				responseJson.put("flag", "0");//添加失败
				responseJson.put("msg", "操作失败，该产品没有BOM清单");
				return responseJson;
			}
		}
		
		//保存信息
		Map<String, Object> resultHp = produceinfoService.updateProduceinfo(form);
		
		responseJson.put("flag", resultHp.get("flag"));//操作成功
		responseJson.put("msg",  resultHp.get("msg"));
		return responseJson;
	}
    
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/delete")
	public Map<String,Object> delete(Produceinfo form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		//删除生产任务
		produceinfoDao.deleteByPrimaryKey(form.getId());
		//删除旧材料
		producematerialDao.deleteByProducecode(form.getProducecode());
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/deleteBatch")
	public Map<String,Object> deleteBatch(@RequestBody List<Produceinfo> formlist) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		for (Produceinfo produceinfo : formlist) {
			//删除材料
			produceinfoDao.deleteByPrimaryKey(produceinfo.getId());
		}

		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/updateOnline")
	public Map<String,Object> updateOnline(@RequestBody List<Produceinfo> formlist) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if(formlist == null || formlist.size()<1){
			responseJson.put("flag", "0");//删除成功
			responseJson.put("msg", "操作失败，请选择需要上线的生产任务！");
			return responseJson;
		}
		
		Produceinfo record = produceinfoDao.selectByPrimaryKey(formlist.get(0).getId());
		
		if(record == null){
			responseJson.put("flag", "0");//删除成功
			responseJson.put("msg", "操作失败，该生产任务不存在！");
			return responseJson;
		}
		
//		if(!"新建".equals(record.getStatus()) && !"已暂停".equals(record.getStatus())){
//			responseJson.put("flag", "0");//删除成功
//			responseJson.put("msg", "操作失败，该生产任务不是新建或已暂停状态！");
//			return responseJson;
//		}
		
		//查询已上线的任务
		Produceinfo productinfo = produceinfoDao.findByLinecode(record.getLinecode());
		if(productinfo!= null){
			responseJson.put("flag", "0");//删除成功
			responseJson.put("msg", "操作失败，该生产线已经有其他的生产任务，请先下线！");
			return responseJson;
		}
		
		record.setStatus("已上线");
		record.setOnlinetime(Tools.getCurrentTime());
		produceinfoDao.updateByPrimaryKeySelective(record);
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/updateOffline")
	public Map<String,Object> updateOffline(@RequestBody List<Produceinfo> formlist) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if(formlist == null || formlist.size()<1){
			responseJson.put("flag", "0");//删除成功
			responseJson.put("msg", "操作失败，请选择需要上线的生产任务！");
			return responseJson;
		}
		
		Produceinfo record = produceinfoDao.selectByPrimaryKey(formlist.get(0).getId());
		
		if(record == null){
			responseJson.put("flag", "0");//删除成功
			responseJson.put("msg", "操作失败，该生产任务不存在！");
			return responseJson;
		}
		
		if("新建".equals(record.getStatus())){
			responseJson.put("flag", "0");//删除成功
			responseJson.put("msg", "操作失败，该生产任务不是已上线状态！");
			return responseJson;
		}
		
		record.setStatus("已下线");
		record.setOfflinetime(Tools.getCurrentTime());
		produceinfoDao.updateByPrimaryKeySelective(record);
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/updatePauseline")
	public Map<String,Object> updatePauseline(@RequestBody List<Produceinfo> formlist) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if(formlist == null || formlist.size()<1){
			responseJson.put("flag", "0");//删除成功
			responseJson.put("msg", "操作失败，请选择需要暂停的生产任务！");
			return responseJson;
		}
		
		Produceinfo record = produceinfoDao.selectByPrimaryKey(formlist.get(0).getId());
		
		if(record == null){
			responseJson.put("flag", "0");//删除成功
			responseJson.put("msg", "操作失败，该生产任务不存在！");
			return responseJson;
		}
		
		if(!"已上线".equals(record.getStatus())){
			responseJson.put("flag", "0");//删除成功
			responseJson.put("msg", "操作失败，该生产任务不是已上线状态！");
			return responseJson;
		}
		
		record.setStatus("已暂停");
		produceinfoDao.updateByPrimaryKeySelective(record);
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
}
